﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;

public partial class Main_Master_Customer_Customer : System.Web.UI.Page
{
    //For the global variable.
    //PAge ID=408
    #region
    Datautility du = new Datautility();
    DataTable dt = new DataTable();
    string strsql = "";
    string LedgerID = "";
    #endregion
    //For the Function, load on the Page Load.
    #region
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpired.aspx");
        }
        string Pagepermission = "select PAGE_ID,PAGE_ADD,PAGE_VIEW,USERID,USERNAME as EmailID from USER_PERMISSION inner join LOGIN on USER_PERMISSION.USER_ID=LOGIN.USERID where USER_ID='" + Session["userid"].ToString() + "' and PAGE_ID=408";
        DataTable dt = du.getdata(Pagepermission);
        if (dt.Rows.Count > 0)
        {
            if (Request.QueryString["action"] != null)
            {
                #region
                if (Request.QueryString["action"].ToString() == "addnew")
                {
                    if (dt.Rows[0]["PAGE_ADD"].ToString() == "True")
                    {
                        PnlAddNew.Visible = true;
                        if (!IsPostBack)
                        {
                            BindSalePerson();
                            BindPriceGroup();
                            BindGroupName();
                            BindTermCondition();
                            BindDDLServices();
                            nameBind();
                            DeliveryNameBind();
                            bindcountry();
                            bindstate();
                            BindTaxType();
                            EnableControl();
                            BindTaxGroupSales();
                            BindTaxGroupPurchase();
                            DDLSubClass.Enabled = false;
                            DDLMainClass.Enabled = false;
                        }
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }
                #endregion
                #region
                else if (Request.QueryString["action"].ToString() == "view")
                {
                    if (dt.Rows[0]["PAGE_VIEW"].ToString() == "True")
                    {
                        if (!IsPostBack)
                        {
                            PnlView.Visible = true;
                            BindGridVendor();
                        }
                    }
                    else
                    {
                        Response.Redirect("~/AccessDenied.aspx");
                    }
                }
                #endregion
            }            
        }
        else
        {
            Response.Redirect("~/AccessDenied.aspx");
        }
    }
    protected void EnableControl()
    {
        strsql = "Select * from IVR_SETTINGS";
        DataTable DTEnable = du.getdata(strsql);
        if (DTEnable.Rows.Count > 0)
        {
            if (DTEnable.Rows[0]["TCS"].ToString() == "True")
            {
                chkApplyTCS.Enabled = true;
            }
            else
            {
                chkApplyTCS.Checked = false;
                chkApplyTCS.Enabled = false;
            }
            lblSalesPersonMain.Text = DTEnable.Rows[0]["OTHSALEPERSONCAP"].ToString();
            //lblSalePerson.Text = DTEnable.Rows[0]["OTHSALEPERSONCAP"].ToString();

        }
    }
    private void BindGroupName()
    {
        DataTable dt = du.getdata("USP_ACC_GetLedgerForCustomer");
        if (dt.Rows.Count > 0)
        {
            DDLGroupName.DataSource = dt;
            DDLGroupName.DataTextField = "GNAME";
            DDLGroupName.DataValueField = "GID";
            DDLGroupName.DataBind();
        }
    }
    private void EmptyTextBoxes(Control parent)
    {
        // Loop through all the controls on the page
        foreach (Control c in parent.Controls)
        {
            // Check and see if it's a textbox
            if ((c.GetType() == typeof(TextBox)))
            {
                // Since its a textbox clear out the text  
                ((TextBox)(c)).Text = "";
            }
            if ((c.GetType() == typeof(DropDownList)))
            {
                // Since its a textbox clear out the text  
                ((DropDownList)(c)).SelectedValue = "0";
            }
            // Now we need to call itself (recursive) because
            // all items (Panel, GroupBox, etc) is a container
            // so we need to check all containers for any
            // textboxes so we can clear them
            if (c.HasControls())
            {
                EmptyTextBoxes(c);
            }
        }
    }
    protected void BindTermCondition()
    {
        strsql = "select TRMID,TRMDESC from ACC_TERMSCONDITIONS Order By TRMDESC ASC";
        DataTable dt = du.getdata(strsql);
        lstTermCondition.DataSource = dt;
        lstTermCondition.DataTextField = "TRMDESC";
        lstTermCondition.DataValueField = "TRMID";
        lstTermCondition.DataBind();
        lstTermCondition.Items.Insert(0, new ListItem("", "0"));
        lstTermCondition.SelectedValue = "0";
    }
    protected void BindDDLServices()
    {
        DDLServices.Items.Add(new ListItem("Debit", "0"));
        DDLServices.Items.Add(new ListItem("Credit", "1"));
        DDLServices.SelectedValue = "0";
    }
    protected void DeliveryNameBind()
    {

        DDLDileveryName.Items.Add(new ListItem("M/s", "0"));
        DDLDileveryName.Items.Add(new ListItem("Mr.", "1"));
        DDLDileveryName.Items.Add(new ListItem("Mrs.", "2"));
        DDLDileveryName.Items.Add(new ListItem("Ms.", "3"));
        DDLDileveryName.Items.Add(new ListItem(" ", " "));
        DDLDileveryName.SelectedIndex = 4;
    }
    protected void bindcountry()
    {
        //strsql = "select CountryId,CountryName from MAS_Country";
        //DataTable dt = du.getdata(strsql);
        //ddlCountry.DataSource = dt;
        //ddlCountry.DataTextField = "CountryName";
        //ddlCountry.DataValueField = "CountryId";
        //ddlCountry.DataBind();
        //DDLDileveryCountry.DataSource = dt;
        //DDLDileveryCountry.DataTextField = "CountryName";
        //DDLDileveryCountry.DataValueField = "CountryId";
        //DDLDileveryCountry.DataBind();

        strsql = "select CountryId,CountryName from Country";
        DataTable dt = du.getdata(strsql);
        ddlCountry.DataSource = dt;
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryId";
        ddlCountry.DataBind();
        DDLDileveryCountry.DataSource = dt;
        DDLDileveryCountry.DataTextField = "CountryName";
        DDLDileveryCountry.DataValueField = "CountryId";
        DDLDileveryCountry.DataBind();

    }
    protected void bindstate()
    {
        strsql = "select StateId,StateName from MAS_State";
        DataTable DT = du.getdata(strsql);
        ddlState.DataSource = DT;
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateId";
        ddlState.DataBind();
        ddlState.Items.Insert(0, new ListItem("---Select State---", "0"));
        ddlState.SelectedValue = "0";
        DDLDileveryState.DataSource = DT;
        DDLDileveryState.DataTextField = "StateName";
        DDLDileveryState.DataValueField = "StateId";
        DDLDileveryState.DataBind();
        DDLDileveryState.Items.Insert(0, new ListItem("---Select State---", "0"));
        DDLDileveryState.SelectedValue = "0";
    }
    protected void BindTaxType()
    {
        strsql = "select TAXTYPEID,TAXTYPE from ACC_TAXTYPE";
        DataTable DT = du.getdata(strsql);
        DDLTaxType.DataSource = DT;
        DDLTaxType.DataTextField = "TAXTYPE";
        DDLTaxType.DataValueField = "TAXTYPEID";
        DDLTaxType.DataBind();
        DDLTaxType.Items.Insert(0, new ListItem("", "0"));
    }
    protected void BindSalePerson()
    {
        strsql = "Select * from ACC_SALESPERSON";
        DataTable DTSale = du.getdata(strsql);
        if (DTSale.Rows.Count > 0)
        {
            //grdSalePerson.DataSource = DTSale;
            //grdSalePerson.DataBind();
            //*************Bind Data to Dropdownlist********//
            ddlSalePerson.DataSource = DTSale;
            ddlSalePerson.DataTextField = "SPRNAME";
            ddlSalePerson.DataValueField = "SPRID";
            ddlSalePerson.DataBind();
            ddlSalePerson.Items.Insert(0, new ListItem("", "0"));
        }

    }
    protected void BindPriceGroup()
    {
        strsql = "Select * from ACC_SALESPRICEGROUP";
        DataTable DTPrice = du.getdata(strsql);
        if (DTPrice.Rows.Count > 0)
        {
            //grdPriceGroup.DataSource = DTPrice;
            //grdPriceGroup.DataBind();
            //*************Bind Data to Dropdownlist********//
            ddlPriceGroup.DataSource = DTPrice;
            ddlPriceGroup.DataTextField = "SPGNAME";
            ddlPriceGroup.DataValueField = "SPGID";
            ddlPriceGroup.DataBind();
            ddlPriceGroup.Items.Insert(0, new ListItem("", "0"));

        }

    }
    protected void nameBind()
    {

        ddlName.Items.Add(new ListItem("M/s", "0"));
        ddlName.Items.Add(new ListItem("Mr.", "1"));
        ddlName.Items.Add(new ListItem("Mrs.", "2"));
        ddlName.Items.Add(new ListItem("Ms.", "3"));
        ddlName.Items.Add(new ListItem(" ", " "));
        ddlName.SelectedIndex = 4;
    }
    protected void BindTaxMainClass()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TAXTYPEID",DDLTaxType.SelectedValue)
                                };

        DataTable DT = du.getdatausingprocedure("USP_ACC_GetTaxMainClassNames", sparam);
        if (DT.Rows.Count > 0)
        {
            DDLMainClass.DataSource = DT;
            DDLMainClass.DataTextField = "MAINCLASSNAME";
            DDLMainClass.DataValueField = "MAINCLASSID";
            DDLMainClass.DataBind();
        }
        else
        {
            DDLMainClass.Enabled = false;

        }

    }
    protected void BindTaxSubClass()
    {

        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TAXTYPEID",DDLTaxType.SelectedValue),
                                    new SqlParameter("@MAINCLASSID",DDLMainClass.SelectedValue)
                                };

        DataTable DT = du.getdatausingprocedure("USP_ACC_GetLedTaxSubClass", sparam);
        if (DT.Rows.Count > 0)
        {
            DDLSubClass.Enabled = true;
            DDLSubClass.DataSource = DT;
            DDLSubClass.DataTextField = "SUBCLASSNAME";
            DDLSubClass.DataValueField = "SUBCLASSID";
            DDLSubClass.DataBind();
        }
        else
        {
            DDLSubClass.Enabled = false;
        }
    }
    protected bool ValidateLedger()
    {
        strsql = "Select * from ACC_LEDMAST Where LNAME='" + TxtName.Text + "'OR LANAME='" + TxtName.Text + "'";
        DataTable DTName = du.getdata(strsql);
        if (DTName.Rows.Count > 0)
        {
            lblMsg.Text = "duplicate Ledger Name";
            TxtName.Focus();
            return false;
        }
        else
        {
            return true;
        }
    }
    protected bool ValidateGroup()
    {
        strsql = "Select * from ACC_GRPMAST Where GANAME='" + TxtName.Text + "' OR GNAME='" + TxtName.Text + "'";
        DataTable DTGName = du.getdata(strsql);
        if (DTGName.Rows.Count > 0)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Group Name Have Already This Name";
            TxtName.Focus();
            return false;
        }
        else
        {
            return true;
        }

    }
    protected bool ValidateLedger1()
    {
        strsql = "Select * from ACC_LEDMAST Where (LANAME='" + TxtAlterName.Text + "' OR LNAME='" + TxtAlterName.Text + "')AND LANAME<>''";
        DataTable DTAName = du.getdata(strsql);
        if (DTAName.Rows.Count > 0)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "duplicate Alternate Ledger Name";
            TxtAlterName.Focus();
            return false;
        }
        else
        {
            return true;
        }
    }
    protected bool ValidateGroup1()
    {
        strsql = "Select * from ACC_GRPMAST Where (GANAME='" + TxtAlterName.Text + "' OR GNAME='" + TxtAlterName.Text + "')AND GANAME<>''";
        DataTable DTGName = du.getdata(strsql);
        if (DTGName.Rows.Count > 0)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = "Group Name Have Already This Alternate Name";
            TxtAlterName.Focus();
            return false;
        }
        else
        {
            return true;
        }

    }
    protected void BindTaxGroupPurchase()
    {
        DataTable DTPurchase = du.getdatausingprocedure("USP_ACC_GetPurchaseMainClass");
        ddlPurchase.DataSource = DTPurchase;
        ddlPurchase.DataTextField = "MAINCLASSNAME";
        ddlPurchase.DataValueField = "MAINCLASSID";
        ddlPurchase.DataBind();
        ddlPurchase.Items.Insert(0, new ListItem("", "0"));
    }
    protected void BindTaxGroupSales()
    {
        DataTable DTSales = du.getdatausingprocedure("USP_ACC_GetSalesMainClass");
        ddlSales.DataSource = DTSales;
        ddlSales.DataTextField = "MAINCLASSNAME";
        ddlSales.DataValueField = "MAINCLASSID";
        ddlSales.DataBind();
        ddlSales.Items.Insert(0, new ListItem("", "0"));
    }
    #endregion
    //For the Save and Close Button function and event.  
    #region
    protected void BtnClose_Click(object sender, EventArgs e)
    {

    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (ValidateLedger1() && ValidateLedger() && ValidateGroup() && ValidateGroup1())
            {
                InsertWithGroup();
                InsertAddressDetails();
                InsertDeliveryDetails();
                InsertContactDetails();
                InsertTermConditions();
                InsertRegistrationDetails();
                InsertOthersDetails();
                InsertTaxDetails();
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Customer Detail Created Successfully";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
            ex.Source += lblMsg.Text;
        }
    }
    protected void InsertWithGroup()
    {
        try
        {
            decimal openingBal = 0;
            openingBal = Convert.ToDecimal(TxtOpeningBal.Text.Trim());
            if (openingBal > 0)
            {
                if (DDLServices.SelectedItem.Text == "Debit")
                {
                    openingBal = 0 - openingBal;
                }
                else if (DDLServices.SelectedItem.Text == "Credit")
                {
                    openingBal = openingBal;
                }
            }
            SqlParameter[] param = {
                                  new SqlParameter("@PGID",DDLGroupName.SelectedValue),
                                  new SqlParameter("@LNAME",TxtName.Text.Trim()),
                                  new SqlParameter("@LANAME",TxtAlterName.Text.Trim()),
                                  new SqlParameter("@ACTOPENING", openingBal),
                                  new SqlParameter("@BILLWISE","0"),
                                  new SqlParameter("@ALWSUB","0"),                                  
                                  new SqlParameter("@TAXAPPLY",ChkTaxType.Checked),
                                  new SqlParameter("@COSTCENTREAPPLY",ChkCost.Checked),
                                  new SqlParameter("@TDSAPPLY",ChkTDS.Checked),
                                  new SqlParameter("@STAPPLY",ChkService.Checked),                                 
                                  new SqlParameter("@CName",Session["username"].ToString()),
                                  new SqlParameter("@VCODE","0")

                              };

            du.executeprocedure("usp_insert_ACC_LEDMAST", param);
            strsql = "Select Ident_Current('ACC_LEDMAST')as Lid";
            DataTable DT = du.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                LedgerID = DT.Rows[0]["LID"].ToString();
                strsql = "Update ACC_LEDMAST set PLID='" + LedgerID.ToString() + "' where LID='" + LedgerID.ToString() + "'";
                int result = du.executesql(strsql);
                if (result > 0)
                {
                    lblMsg.Attributes.Remove("class");
                    lblMsg.Attributes.Add("class", "true");
                    lblMsg.Text = "Customer Detail Created Successfully...";
                }
            }
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;

        }
    }
    protected void InsertContactDetails()
    {
        if (txtContactPerson.Text != "")
        {
            try
            {
                SqlParameter[] sparam = 
                                        { 
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@PERSON",txtContactPerson.Text.Trim()),
                                            new SqlParameter("@POSITION",txtContactPosition.Text.Trim()),
                                            new SqlParameter("@PHONENUM",txtContactPhone.Text.Trim()),
                                            new SqlParameter("@MOBILENUM",txtContactMobile.Text.Trim()),
                                            new SqlParameter("@EMAIL",txtContactEmail.Text.Trim()),
                                            new SqlParameter("@FAXNUM",txtContactFax.Text.Trim()),
                                            new SqlParameter("@REMARKS",txtContactRemarks.Text.Trim()),
                                            new SqlParameter("@CNAME",Session["username"].ToString()),
                                        };
                du.executeprocedure("USP_ACC_SaveLedgerContactDetails", sparam);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Delivery Details Created Successfully...";
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
    protected void InsertDeliveryDetails()
    {
        string LstDate = "";
        string CstDate = "";
        string CepDate = "";

        if (txtDeliveryVatDate.Text.Trim() != string.Empty)
        {
            string[] lst = txtDeliveryVatDate.Text.ToString().Split(new char[] { '/' });
            LstDate = lst[1].ToString() + "/" + lst[0].ToString() + "/" + lst[2].ToString();
        }
        if (txtDeliveryInterStateDate.Text.Trim() != string.Empty)
        {
            string[] cst = txtDeliveryInterStateDate.Text.ToString().Split(new char[] { '/' });
            CstDate = cst[1].ToString() + "/" + cst[0].ToString() + "/" + cst[2].ToString();
        }

        if (txtDeliveryCENVATDate.Text.Trim() != string.Empty)
        {
            string[] cep = txtDeliveryCENVATDate.Text.ToString().Split(new char[] { '/' });
            CepDate = cep[1].ToString() + "/" + cep[0].ToString() + "/" + cep[2].ToString();
        }
        if (txtDileveryName.Text != "")
        {
            try
            {

                SqlParameter[] sparam = 
                                        {
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@TITLE",DDLDileveryName.SelectedItem.Text.Trim()),
                                            new SqlParameter("@CONTACTNAME",txtDileveryName.Text.Trim()),
                                            new SqlParameter("@ADD1",txtDileveryAddress1.Text.Trim()),
                                            new SqlParameter("@ADD2",txtDileveryAddress2.Text.Trim()),
                                            new SqlParameter("@ADD3",txtDileveryAddress3.Text.Trim()),
                                            new SqlParameter("@ADD4",txtDileveryAddress4.Text.Trim()),
                                            new SqlParameter("@PINCODE",txtDileveryPincode.Text.Trim()),
                                              new SqlParameter("@place", TxtDileveryPlace.Text.Trim()),
                                            new SqlParameter("@fax", TxtDileveryFax.Text.Trim()),
                                            new SqlParameter("@phone", TxtDileveryPhone.Text.Trim()),
                                            
                                            new SqlParameter("@STATEID",DDLDileveryState.SelectedValue),
                                            new SqlParameter("@COUNTRY",DDLDileveryCountry.SelectedItem.Text.Trim()),
                                            new SqlParameter("@LSTNUM",txtVat.Text.Trim()),
                                            new SqlParameter("@LSTDATE",LstDate.ToString()),
                                            new SqlParameter("@CSTNUM",txtDeliveryInterState.Text.Trim()),
                                            new SqlParameter("@CSTDATE",CstDate.ToString()),
                                            new SqlParameter("@CEPANUM",txtDeliveryCENVAT.Text.Trim()),
                                            new SqlParameter("@CEPADATE",CepDate.ToString()),
                                            new SqlParameter("@CName",Session["username"].ToString())
                                        };
                int result = du.executeprocedure("USP_ACC_SaveLedgerDeliveryAddressDetails", sparam);
                if (result > 0)
                {
                    lblMsg.Attributes.Remove("class");
                    lblMsg.Attributes.Add("class", "true");
                    lblMsg.Text = "Delivery Details Created Successfully...";
                }
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
    protected void InsertAddressDetails()
    {
        if (txtAddName.Text != string.Empty)
        {
            try
            {

                SqlParameter[] sparam = 
                                        { 
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@TITLE",ddlName.SelectedItem.Text.Trim()),
                                            new SqlParameter("@LNAME",txtAddName.Text.Trim()),
                                            new SqlParameter("@ADDRESS1",txtAddress1.Text.Trim()),
                                            new SqlParameter("@ADDRESS2",txtAddress2.Text.Trim()),
                                            new SqlParameter("@ADDRESS3",txtAddress3.Text.Trim()),
                                            new SqlParameter("@PLACE",txtAddPlace.Text.Trim()),
                                            new SqlParameter("@PINCODE",txtPin.Text.Trim()),
                                            new SqlParameter("@STATEID",ddlState.SelectedValue),
                                            new SqlParameter("@COUNTRY",ddlCountry.SelectedItem.Text),
                                            new SqlParameter("@WEBSITE",txtWebsite.Text.Trim()),
                                            new SqlParameter("@EMAIL",txtEmail.Text.Trim()),
                                            new SqlParameter("@PHONE",txtPhone.Text.Trim()),
                                            new SqlParameter("@FAX",txtFax.Text.Trim()),
                                            new SqlParameter("@CNAME",Session["username"].ToString())
                                           
                                        };
                du.executeprocedure("USP_ACC_SaveLedgerAddressDetails", sparam);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Address Details Created Successfully...";

            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
    protected void InsertTermConditions()
    {

        if (Convert.ToInt32(lstTermCondition.SelectedValue) > 0)
        {
            try
            {
                SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@TRMID",lstTermCondition.SelectedValue)
                                        };
                du.executeprocedure("USP_ACC_SaveLedgerTermsCondition", sparam);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Terms and Conditions Details Created Successfully...";
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
    protected void InsertRegistrationDetails()
    {
        string PANDate = "";
        string VatDate = "";
        string CSTDate = "";
        string TDSDate = "";
        string STDate = "";
        string LTDate = "";
        string CENVATDate = "";
        if (txtRegPANDate.Text != "")
        {
            string[] pan = txtRegPANDate.Text.ToString().Split(new char[] { '/' });
            PANDate = pan[1].ToString() + "/" + pan[0].ToString() + "/" + pan[2].ToString();
        }
        if (txtRegVATDate.Text != "")
        {
            string[] vat = txtRegVATDate.Text.ToString().Split(new char[] { '/' });
            VatDate = vat[1].ToString() + "/" + vat[0].ToString() + "/" + vat[2].ToString();
        }
        if (txtRegInterStateDate.Text != "")
        {
            string[] cst = txtRegInterStateDate.Text.ToString().Split(new char[] { '/' });
            CSTDate = cst[1].ToString() + "/" + cst[0].ToString() + "/" + cst[2].ToString();
        }
        if (txtRegTaxDeductionDate.Text != "")
        {
            string[] tds = txtRegTaxDeductionDate.Text.ToString().Split(new char[] { '/' });
            TDSDate = tds[1].ToString() + "/" + tds[0].ToString() + "/" + tds[2].ToString();
        }
        if (txtRegServiceTaxDate.Text != "")
        {
            string[] st = txtRegServiceTaxDate.Text.ToString().Split(new char[] { '/' });
            STDate = st[1].ToString() + "/" + st[0].ToString() + "/" + st[2].ToString();
        }
        if (txtRegLuxuryDate.Text != "")
        {
            string[] lt = txtRegLuxuryDate.Text.ToString().Split(new char[] { '/' });
            LTDate = lt[1].ToString() + "/" + lt[0].ToString() + "/" + lt[2].ToString();
        }
        if (txtRegCVNNETDate.Text != "")
        {
            string[] cenvat = txtRegCVNNETDate.Text.ToString().Split(new char[] { '/' });
            CENVATDate = cenvat[1].ToString() + "/" + cenvat[0].ToString() + "/" + cenvat[2].ToString();
        }
        try
        {

            SqlParameter[] sparam = 
                                        {
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@REGID","0"),
                                            new SqlParameter("@PAN",txtRegPAN.Text.Trim()),
                                            new SqlParameter("@PANDATE",PANDate.ToString()),
                                            new SqlParameter("@VATTIN",txtRegVAT.Text.Trim()),
                                            new SqlParameter("@VATTINDATE",VatDate.ToString()),
                                            new SqlParameter("@CSTNO",txtRegInterState.Text.Trim()),
                                            new SqlParameter("@CSTDATE",CSTDate.ToString()),
                                            new SqlParameter("@TDSNO",txtRegTaxDeduction.Text.Trim()),
                                            new SqlParameter("@TDSDATE",TDSDate.ToString()),
                                            new SqlParameter("@SERTNO",txtRegServicetax.Text.Trim()),
                                            new SqlParameter("@SERTNDATE",STDate.ToString()),
                                            new SqlParameter("@LTNO",txtRegLuxury.Text.Trim()),
                                            new SqlParameter("@LTNDATE",LTDate.ToString()),
                                            new SqlParameter("@CEPAN",txtRegCVNNET.Text.Trim()),
                                            new SqlParameter("@CEPANDATE",CENVATDate.ToString()),
                                            new SqlParameter("@PANREF",txtRegPanRef.Text.Trim()),
                                            new SqlParameter("@CName",Session["username"].ToString())
                                        };
            du.executeprocedure("USP_ACC_SaveRegistrationDetails", sparam);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Terms and Conditions Details Created Successfully...";
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }
    }
    protected void InsertOthersDetails()
    {
        try
        {
            SqlParameter[] sparam = 
                                        { 
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@ACTIVE",CHKAvtive.Checked),
                                            new SqlParameter("@TCS",chkApplyTCS.Checked),
                                            new SqlParameter("@CREDITPERIOD",txtCreditPeriod.Text.Trim()),
                                            new SqlParameter("@CREDITLIMIT",txtCreditLimit.Text.Trim()),
                                            new SqlParameter("@PURTAXMAINCLASSID",ddlPurchase.SelectedValue),
                                            new SqlParameter("@SALETAXMAINCLASSID",ddlSales.SelectedValue),
                                            new SqlParameter("@SPRID",ddlSalePerson.SelectedValue),
                                            new SqlParameter("@SPGID",ddlPriceGroup.SelectedValue),
                                            new SqlParameter("@SUBTAXID","0"),
                                            new SqlParameter("@CName",Session["username"].ToString())
                                        };
            du.executeprocedure("USP_ACC_SaveLedgerOtherDetails", sparam);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Other Details Created Successfully...";
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }
    }
    protected void InsertTaxDetails()
    {
        if (Convert.ToInt32(DDLTaxType.SelectedValue) > 0)
        {
            try
            {

                SqlParameter[] sparam = 
                                        { 
                                            new SqlParameter("@LID",LedgerID.ToString()),
                                            new SqlParameter("@TAXTYPEID",DDLTaxType.SelectedValue),
                                            new SqlParameter("@MAINCLASSID",DDLMainClass.SelectedValue),
                                            new SqlParameter("@SUBCLASSID",DDLSubClass.SelectedValue),
                                           new SqlParameter("@CName",Session["username"].ToString())
                                        };
                du.executeprocedure("USP_ACC_SaveLedgerTaxDetails", sparam);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Tax Details Created Successfully...";
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
    }
    #endregion
    //For the Selected Index of the DropDown.
    #region
    protected void ddlPurchase_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((Convert.ToInt32(ddlPurchase.SelectedValue) == 1) || (Convert.ToInt32(ddlPurchase.SelectedValue) == 2))
        {
            ddlSales.SelectedValue = "3";
        }
        else if (Convert.ToInt32(ddlPurchase.SelectedValue) == 5)
        {
            ddlSales.SelectedValue = "6";
        }
        else
        {
            ddlSales.SelectedValue = "0";
        }
    }
    protected void ddlSales_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(ddlSales.SelectedValue) == 3)
        {
            ddlPurchase.SelectedValue = "1";
        }
        else if (Convert.ToInt32(ddlSales.SelectedValue) == 6)
        {
            ddlPurchase.SelectedValue = "5";
        }
        else
        {
            ddlPurchase.SelectedValue = "0";
        }
    }
    protected void DDLTaxType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(DDLTaxType.SelectedValue) > 0)
        {
            DDLMainClass.Enabled = true;
            BindTaxMainClass();

            DDLSubClass.Items.Clear();
            DDLSubClass.Items.Insert(0, new ListItem("", "0"));
            DDLSubClass.Enabled = false;

        }
        else
        {
            DDLMainClass.Items.Clear();
            DDLMainClass.Items.Insert(0, new ListItem("", "0"));
            DDLMainClass.Enabled = false;

        }
    }
    protected void DDLMainClass_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (Convert.ToInt32(DDLMainClass.SelectedValue) > 0)
        {
            DDLSubClass.Enabled = true;
            BindTaxSubClass();

        }
        else
        {
            DDLSubClass.Items.Clear();
            DDLSubClass.Items.Insert(0, new ListItem("", "0"));
            DDLSubClass.Enabled = false;
        }
    }
    #endregion
    //For the view of the Vendor Detial and Gridview Events
    #region
    protected void BindGridVendor()
    {
        dt = du.getdatausingprocedure("usp_Acc_DisplayCustomer");
        if (dt.Rows.Count > 0)
        {
            DataView dv = dt.DefaultView;
            if (this.ViewState["SortExpression"] != null)
            {
                dv.Sort = string.Format("{0} {1}", ViewState["SortExpression"].ToString(), this.ViewState["SortOrder"].ToString());
            }
            GrdCustomer.DataSource = dt;
            GrdCustomer.DataBind();
        }
    }
    protected void GrdCustomer_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdCustomer.PageIndex = e.NewPageIndex;
        BindGridVendor();
    }
    protected void GrdCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        string key = GrdCustomer.SelectedDataKey.Value.ToString();
        Response.Redirect("CustomerView.aspx?lid=" + key);
    }
    protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        //if (chk.Checked == true)
        //{
        //    string name = txtAddName.Text.ToString();
        //    string ddlname = ddlName.SelectedItem.Text;
        //    string add1 = txtAddress1.Text.ToString();
        //    string add2 = txtAddress2.Text.ToString();
        //    string add3 = txtAddress3.Text.ToString();
        //    string place = txtAddPlace.Text.ToString();
        //    string pin = Convert.ToString(txtPin.Text.ToString());
        //    string state = ddlState.SelectedItem.Text;
        //    string country = ddlCountry.SelectedItem.Text;
        //    txtDileveryName.Text = name;
        //    DDLDileveryName.SelectedItem.Text = ddlname;
        //    txtDileveryAddress1.Text = add1;
        //    txtDileveryAddress2.Text = add2;
        //    txtDileveryAddress3.Text = add3;
        //    DDLDileveryPlace.Text = place;
        //    txtDileveryPincode.Text = pin;
        //    DDLDileveryState.SelectedItem.Text = state;
        //    DDLDileveryCountry.SelectedItem.Text = country;
        //}
        //else if (chk.Checked == false)
        //{
        //    txtDileveryName.Text = "";
        //    DDLDileveryName.SelectedItem.Text = "";
        //    txtDileveryAddress1.Text = "";
        //    txtDileveryAddress2.Text = "";
        //    txtDileveryAddress3.Text = "";
        //    DDLDileveryPlace.Text = "";
        //    txtDileveryPincode.Text = "";
        //    DDLDileveryState.SelectedItem.Text = "";
        //    DDLDileveryCountry.SelectedItem.Text = "";
        //}
    }
    protected void GrdCustomer_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Sort"))
        {
            if (ViewState["SortExpression"] != null)
            {
                if (this.ViewState["SortExpression"].ToString() == e.CommandArgument.ToString())
                {
                    if (ViewState["SortOrder"].ToString() == "ASC")
                        ViewState["SortOrder"] = "DESC";
                    else
                        ViewState["SortOrder"] = "ASC";
                }
                else
                {
                    ViewState["SortOrder"] = "ASC";
                    ViewState["SortExpression"] = e.CommandArgument.ToString();
                }
            }
            else
            {
                ViewState["SortExpression"] = e.CommandArgument.ToString();
                ViewState["SortOrder"] = "ASC";
            }
        }
        BindGridVendor();
    }
    #endregion
    //For the  Gridview To Export to Excel
    #region
    protected void imgxl_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            string SqlCustomer = "Select ACC_LEDADDRESS.LNAME as CustomerName,ACC_LEDADDRESS.ADDRESS1+''+ACC_LEDADDRESS.ADDRESS2+''+ACC_LEDADDRESS.ADDRESS3 as CustomerAddress,ACC_LEDADDRESS.PLACE as City,ACC_LEDADDRESS.PINCODE as Pincode,ACC_LEDADDRESS.COUNTRY as Country,ACC_LEDADDRESS.Website as Website,ACC_LEDADDRESS.EMAIL as Email,ACC_LEDADDRESS.PHONE as Phone,ACC_LEDADDRESS.FAX as Fax,ACC_LEDREGDETAILS.PAN as PanNo,ACC_LEDREGDETAILS.VATTIN as VatTin,ACC_LEDREGDETAILS.CSTNO as CSTNo, ACC_LEDCONTACTDETAILS.PERSON As ContactPerson,ACC_LEDCONTACTDETAILS.POSITION as Designation,ACC_LEDCONTACTDETAILS.PHONENUM as PhoneNo,ACC_LEDCONTACTDETAILS.MOBILENUM as MobileNo,ACC_LEDCONTACTDETAILS.EMAIL as EmailID,ACC_LEDCONTACTDETAILS.FAXNUM as FaxNo from ACC_LEDCONTACTDETAILS inner join ACC_LEDREGDETAILS on ACC_LEDREGDETAILS.LID=ACC_LEDCONTACTDETAILS.LID inner join ACC_LEDADDRESS on ACC_LEDADDRESS.LID=ACC_LEDREGDETAILS.LID inner join ACC_LEDMAST on ACC_LEDMAST.LID=ACC_LEDADDRESS.LID where ACC_LEDMAST.PGID=54";
            DataTable Dt = du.getdata(SqlCustomer);
            grdCustomerDetails.DataSource = Dt;
            grdCustomerDetails.DataBind();
            grdCustomerDetails.ShowFooter = false;
            grdCustomerDetails.AllowPaging = false;
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customer-Detail.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grdCustomerDetails.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // base.VerifyRenderingInServerForm(control);
    }
    #endregion
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            BindGridVendor();
            dt.DefaultView.RowFilter = "LNAME like '%" + txtsearchCustomer.Text + "%'";
            if (dt.Rows.Count > 0)
            {
                GrdCustomer.DataSource = dt;
                GrdCustomer.DataBind();
            }
            else
            {
                lblMsg.Text = "No Record Found !";
                lblMsg.ForeColor = System.Drawing.Color.Red;
                lblMsg.Attributes.Add("style", "display:block");
            }
        }
        catch (Exception Ex)
        {
        }
    }
}
